<?php

/**
 * 筛选条件配置
 */

namespace app\api\model;

use think\Db;
use think\Model;

class IntervalConfig extends Model {

    //拍卖状态
    const A_STATUS1 = 1;
    const A_STATUS2 = 2;
    const A_STATUS3 = 3;
    const A_STATUS4 = 4;
    const A_STATUS5 = 5;
    const A_STATUS6 = 6;
    const A_STATUS7 = 7;
    public static $a_status = [
        ['id' => self::A_STATUS1, 'name' => '待开始'],
        ['id' => self::A_STATUS2, 'name' => '进行中'],
        ['id' => self::A_STATUS3, 'name' => '已成交'],
        ['id' => self::A_STATUS4, 'name' => '已流拍'],
        ['id' => self::A_STATUS5, 'name' => '已撤回'],
        ['id' => self::A_STATUS6, 'name' => '已终'],
        ['id' => self::A_STATUS7, 'name' => '已暂缓'],
    ];

    //拍卖阶段
    const A_TYPE1 = 1;
    const A_TYPE2 = 2;
    const A_TYPE3 = 3;
    const A_TYPE4 = 4;
    const A_TYPE5 = 5;
    const A_TYPE6 = 6;
    public static $a_type = [
        ['id' => self::A_TYPE1, 'name' => '一拍'],
        ['id' => self::A_TYPE2, 'name' => '二拍'],
//        ['id' => self::A_TYPE3, 'name' => '三拍'],
        ['id' => self::A_TYPE4, 'name' => '变卖'],
        ['id' => self::A_TYPE5, 'name' => '重新拍卖'],
//        ['id' => self::A_TYPE6, 'name' => '破产'],
    ];

    //户型
    const BEDROOM1 = 1;
    const BEDROOM2 = 2;
    const BEDROOM3 = 3;
    const BEDROOM4 = 4;
    const BEDROOM5 = 5;
    public static $bedroom = [
        ['id' => self::BEDROOM1, 'name' => '一居室'],
        ['id' => self::BEDROOM2, 'name' => '二居室'],
        ['id' => self::BEDROOM3, 'name' => '三居室'],
        ['id' => self::BEDROOM4, 'name' => '四居室'],
        ['id' => self::BEDROOM5, 'name' => '五居室以上'],
    ];

//排序
    const ORDERBBY1 = 1;
    const ORDERBBY2 = 2;
    const ORDERBBY3 = 3;
    const ORDERBBY4 = 4;
    const ORDERBBY5 = 5;
    const ORDERBBY6 = 6;
    const ORDERBBY7 = 7;
    const ORDERBBY8 = 8;
    public static $orderby = [
        ['id' => self::ORDERBBY1, 'name' => '默认排序'],
        ['id' => self::ORDERBBY2, 'name' => '按价格从高到低'],
        ['id' => self::ORDERBBY3, 'name' => '按价格从低到高'],
        ['id' => self::ORDERBBY4, 'name' => '按折扣从高到低'],
        ['id' => self::ORDERBBY5, 'name' => '按折扣从低到高'],
        ['id' => self::ORDERBBY6, 'name' => '开拍时间由远及近'],
        ['id' => self::ORDERBBY7, 'name' => '开拍时间由近及远'],
        ['id' => self::ORDERBBY8, 'name' => '最近发布'],
    ];

    //装修
    const RENOVATION0 = 0;
    const RENOVATION1 = 1;
    const RENOVATION2 = 2;
    const RENOVATION3 = 3;
    const RENOVATION4 = 4;
    public static $renovation_type = [
        ['name' => '未知', 'key' => self::RENOVATION0],
        ['name' => '清水', 'key' => self::RENOVATION1],
        ['name' => '简装', 'key' => self::RENOVATION2],
        ['name' => '中装', 'key' => self::RENOVATION3],
        ['name' => '精装', 'key' => self::RENOVATION4],
    ];

    //电梯
    const elevator1 = 1;
    const elevator2 = 2;
    const elevator3 = 3;
    public static $elevator = [
        ['name' => '电梯', 'key' => self::elevator1],
        ['name' => '楼梯', 'key' => self::elevator2],
        ['name' => '未知', 'key' => self::elevator3],
    ];

    const CONFIG_TYPE1 = 1;//价格
    const CONFIG_TYPE2 = 2;//面积
    const CONFIG_TYPE3 = 3;//折扣
    const CONFIG_TYPE4 = 4;//楼龄
    const CONFIG_TYPE5 = 5;//楼层
    const CONFIG_TYPE6 = 6;//户型


    /**
     *区间数据
     */
    public static function all_intervel($city_code) {
        $interval_config = Db::name('interval_config')
            ->order('min', 'asc')
            ->whereOr('city_code', 0)
            ->whereOr('city_code', $city_code)
            ->field('type,name,min,max')
            ->select();
        $i = 0;
        do {
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE1) {
                $all_intervel['price_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE2) {
                $all_intervel['build_area_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE3) {
                $all_intervel['discount_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE4) {
                $all_intervel['house_age'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE5) {
                $all_intervel['house_floor'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE6) {
                $all_intervel['house_door'][] = self::deal($interval_config[$i]);
            }
            $i++;
        } while ($i < count($interval_config));
        return $all_intervel;
    }

    /**
     * 区间处理
     * @param $arr
     * @return mixed
     */
    public static function deal($arr) {
        $arr['key'] = $arr['min'] . '-' . $arr['max'];
        unset($arr['min']);
        unset($arr['max']);
        unset($arr['type']);
        return $arr;
    }

    /**
     * 获取配置
     * @param $min
     * @param $max
     * @param $type
     * @return mixed
     */
    public static function getConfig_name($min, $max, $type) {
         $re=Db::name('interval_config')->where('min', $min)->where('max', $max)->where('type', $type)->value('name');
         return $re??'';

    }

}
